<?php
header("content-type:text/html;charset=utf-8");
//include "../is_login.php";
include "../public/config/config.php";
include "../public/include/funcs.php";
$link=conn($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd,'fenxiao',$cfg_dbchar);
//查询，修改版
function select2($link,$tableName,$where="",$cols="*",$limit=""){
	if($where!=""){
		$where="where ".$where;
	}
	if($limit!=""){
		$limit="limit ".$limit;
	}
	$sql="select $cols from $tableName $where $limit";
	$re=mysqli_query($link,$sql);
	if($re==FALSE){
		echo "sql拼写有误！！";
		echo $sql;
	}else{
		$arr=array();
		while($row=mysqli_fetch_assoc($re)){
			$arr[$row['id']]=$row;
		}
		if(empty($arr)){
			//echo "没有符合条件的记录";
		}else{
			return $arr;
		}
	}
}
$arr = select2($link, 'kehu_mo');

//echo "<pre>";
//print_r($arr);
//echo "</pre>";

function get_tree($arr){
	foreach($arr as $v){
		$arr[$v['pid']]['children'][]= &$arr[$v['id']];
	}
	return $arr[0];
}

$arr_tree = get_tree($arr);
$arr_tree['tname'] = '康承贝健';

//echo "<pre>";
//print_r($arr_tree);
//echo "</pre>";

$json = json_encode($arr_tree);
//var_dump($json);


?>
<html>
<head>
	<meta charset="UTF-8">
	<title>完整数状图</title>
	<script src="d3_tree/d3/d3.v3.min.js" charset="utf-8"></script>
	<style>
		svg{width: 1880px;height: 900px;display: block;}
		/*svg{width: 10000px;height: 10000px;display: block;}*/
		path.link{stroke: #aaa;stroke-width: 1.5px;fill:transparent;}
		.node circle{fill:#fff;stroke:steelblue;stroke-width: 1.5px;}
	</style>
</head>
<body>
	<a target="_blank" href="d3_tree/index_all.php">查看树状图样例</a>
<svg></svg>	
</body>
<script type="text/javascript">
var tree = d3.layout.tree()
			.size([900, 1500])
//			.size([3000, 1500])
			.separation(function(a, b) { return (a.parent == b.parent ? 1 : 2); });
d3.json("diy.json", function(error, root) {
	var root2 = '<?php echo $json;?>';
	var root3 = JSON.parse(root2);
	console.log(root3);
	var nodes = tree.nodes(root3);
	var links = tree.links(nodes);
//	console.log(nodes);
//	console.log(links);
//console.log(root3);
	var diagonal = d3.svg.diagonal().projection(function(d) { return [d.y, d.x]; });
	var svg = d3.select('svg').append('g').attr("transform", "translate(80,0)");
	var link = svg.selectAll(".link").data(links).enter().append("path").attr("class", "link").attr("d", diagonal);
	var node = svg.selectAll('.node').data(nodes).enter().append('g').attr('class','node').attr('transform',function (d) {return "translate(" + d.y + "," + d.x + ")";});
	node.append('circle').attr('r',5);
	node.append('text').attr("dx", function(d) { return d.children ? -8 : 8; }).attr("dy", 3).style("text-anchor", function(d) { return d.children ? "end" : "start"; })
		.append("a").attr('href',"https://www.baidu.com/").text(function(d) {
			var u_name =  d.tname ? d.tname : d.uname;
			return u_name ? u_name : d.id; 
			});
})
</script>
</html>